﻿/// <reference path="../jquery-1.4.1-vsdoc.js" />
///  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
///  Dual licensed under the MIT and GPL licenses:
///  http://www.opensource.org/licenses/mit-license.php
///  http://www.gnu.org/licenses/gpl.html

(function ($) {
    $.widget("ui.fileExplorer", {
        options: {
            treeview: null,
            listview: null,
            toolbar: null,
            //populatedUrl: null,
            imgPath: "",
            rootPath: ""
            //            createAction: null,
            //            deleteAction: null,
            //            uploadAction: null,
            //            downloadAction: null
        },
        _create: function () {
            var self = this, opts = this.options, el = this.element, _tv = null, _lv = null, _dt = null, _ft = null;

            //binding the ui elements and plugins
            if (opts.treeview) {
                _tv = $(opts.treeview, el);
                if (_tv.length)
                    this.treeviewElement = _tv;
                _tv.bind("click", function (event) {
                    _tv.treeview("option", "selectedNode", null);
                });
            }

            if (opts.listview) {
                var _lv = $(opts.listview, el);
                if (_lv.length)
                    this.listviewElement = _lv;
            }

            if (opts.toolbar) {
                _dt = $(opts.toolbar, el);
                if (_dt.length) this.toolbarElement = _dt;
            }

            self._initCommands();

            _dt.toolbar({ toolpanes: [{
                commands: [
            self.commands.createFolder,
            self.commands.deleteFolder,
            self.commands.spliter,
            self.commands.upload,
            self.commands.deleteFile,
            self.commands.download
            ]
            }],
                command: function (event, cmd) {
                    if ($.isFunction(self[cmd.name]))
                        self[cmd.name](cmd);
                }
            });

            var _uploadCmd = $("li[cmdID='uploadCommand']", _dt).find("a");
            self._initUpload(_uploadCmd);
            _tv.treeview({
                url: opts.rootPath,
                httpMethod: "PLIST",
                applyClassTo: ".ui-node-text",
                selectedClass: "ui-node-text-selected",
                baseImageUrl: opts.imgPath,
                //                beforeNodeLoad: function (_node, _ajaxOptions) {
                //                    _ajaxOptions.url = _node.element.attr("rel");
                //                    _ajaxOptions.type = "PLIST";
                //                },
                select: function (event, ui) {
                    //self._uploader.setData({ "Destination": ui.element.attr("rel") });
                    self._uploader._settings.action = ui.element.attr("rel");
                    //self._initUpload(_uploadCmd);
                    if (ui.element.attr("rel")) {
                        uiHelper.blockUI(_lv);
                        $.ajax({
                            url: ui.element.attr("rel"),
                            type: "FLIST",
                            success: function (_html) {
                                _lv.html(_html);
                                var _list = $(">div", _lv);
                                if (_list.length) {
                                    _list.css({
                                        "overflow": "scroll",
                                        "height": "100%",
                                        "width": "100%"
                                    });
                                }
                                uiHelper.unblockUI(_lv);
                                self.listviewElement = _list;
                            }
                        });
                    }
                }
            });
        },
        _initCommands: function () {
            var self = this;
            this.commands = {
                spliter: { type: "spliter" },
                createFolder: { type: "textbutton", text: portal.res.AddFolder, icon: "ui-icon ui-icon-plus", name: "createdir" },
                deleteFolder: { type: "textbutton", text: portal.res.DeleteFolder, icon: "ui-icon ui-icon-minus", name: "removedir" },
                upload: { id: "uploadCommand", type: "textbutton", text: portal.res.UploadFile, icon: "ui-icon ui-icon-arrowreturnthick-1-n", name: "uploadFile" },
                deleteFile: { type: "textbutton", text: portal.res.DeleteFile, icon: "ui-icon ui-icon-trash", name: "deleteFile" },
                download: { type: "textbutton", text: portal.res.Download, icon: "ui-icon ui-icon-arrowreturnthick-1-s", name: "downloadFile" }
            }
        },
        destroy: function () {
            this.listviewElement.listview("destroy");
            this.treeviewElement.treeview("destroy");
            this.listviewElement = null;
            this.treeviewElement = null;
            $.Widget.prototype.destroy.call(this);
        },
        deleteFile: function () {
            var _lv = this.listviewElement, self = this,
            _item = _lv.listview("option", "selectedItem")
            if (!_item) {
                uiHelper.showMsg(portal.res.Info, portal.res.PleaseSelectFile);
                return;
            }

            uiHelper.confirm(portal.res.Question, portal.res.ConfirmDeleteFile.replace("{0}", _item.value), function () {
                $.ajax({
                    url: _item.value,
                    type: "DELETE",
                    success: function (response) {
                        if (response == "OK") {
                            _item.item.unbind();
                            _item.item.remove();
                        }
                    }
                });
            });
        },
        downloadFile: function () {
            var _lv = this.listviewElement,
            _item = _lv.listview("option", "selectedItem")
            if (!_item) {
                uiHelper.showMsg(portal.res.Info, portal.res.PleaseSelectFile);
                return;
            }
            window.open(_item.value, "__downloadwin",
        "height=200, width=300, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no");
        },
        _getSelectedPath: function () {
            var _tv = this.treeviewElement, opts = this.options,
            _sel = _tv.treeview("option", "selectedNode"),
            _selVal = opts.rootPath;
            if (_sel.element) _selVal = _sel.element.attr("rel");
            return _selVal;
        },
        _initUpload: function (el) {
            var self = this,
            _path = self._getSelectedPath();
           // alert(_path);
            self._uploader = new AjaxUpload(el, {
                action: _path,
                name: "PostedFile",
                autoSubmit:true,
                onChange: function () {
                    uiHelper.blockUI(self.element);
                },
                onComplete: function (file, response) {
                    uiHelper.unblockUI(self.element);
                    if (response == "OK") {
                        var _node = self.treeviewElement.treeview("option", "selectedNode");
                        _node.contentElement.click();
                    }
                    else {
                        uiHelper.showMsg(portal.res.Error, response);
                    }
                }
            });
        },
        removedir: function () {
            var _tv = this.treeviewElement, self = this,
           _sel = _tv.treeview("option", "selectedNode");

            if (!_sel) {
                uiHelper.showMsg(portal.res.Info, portal.res.PleaseSelectFolder);
                return;
            }

            uiHelper.confirm(portal.res.Question, portal.res.ConfirmDeleteFolder.replace("{0}", _sel.element.attr("rel")), function () {
                $.ajax({
                    url: _sel.element.attr("rel"),
                    type: "DELETE",
                    success: function (response) {
                        if (response == "OK") {
                            _sel.element.remove();
                        }
                    }
                });
            });
        },
        createdir: function () {
            var _tv = this.treeviewElement, opts = this.options,
            _sel = this.treeviewElement.treeview("option", "selectedNode"),
            _selVal = opts.rootPath;
            if (_sel) _selVal = _sel.element.attr("rel");

            uiHelper.inputDialog(portal.res.CreateFolderIn + urlHelper.decode(_selVal), portal.res.PleaseInputFolderName, function (strFolder) {
                if (strFolder.length == 0)
                    return;

                if (_selVal.substr(_selVal.length - 1, 1) == "/")
                    strFolder = _selVal + strFolder;
                else
                    strFolder = _selVal + "/" + strFolder;

                //data: { path: strFolder },_sel.element.attr("rel"),
                $.ajax({
                    url: strFolder,
                    type: "MKCOL",
                    success: function (html) {
                        if (html) {
                            if (_sel)
                                _sel.add(html);
                            else
                                _tv.treeview("add", html);
                        }
                    }
                });
            });
        }
    });

})(jQuery);   